import Vue from "vue";
import VueRouter from "vue-router";
import Main from "../Views/Main";
import Home from "../Views/Home.vue";
 import User from "../Views/User.vue";
import PageOne from "../Views/PageOne.vue";
import PageTwo from "../Views/PageTwo.vue";
import Login from "../Views/Login.vue";
import Cookie from "js-cookie";
import { Message  } from "element-ui";
// import { component } from "vue/types/umd";

Vue.use(VueRouter);

// 在这个地方添加这个方法进行设置这个出现问题的界面进行这个设置

const originalPush = VueRouter.prototype.push
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const routes = [
    // 主路由
    {
        path: "/",
        name: "Main",
        component: Main,
        // redirect: "/home", // 重定向
        children: [
            // 子路由
            { path: "/home", name: "home", component: Home }, // 首页
            { path: "/user", name: "user", component: User }, // 用户管理
            { path: "/page1", name: "page1", component: PageOne }, // 页面1
            { path: "/page2", name: "page2", component: PageTwo }, // 页面2
            // 系统用户
            {
                path: "/system_user",
                name: "system_user",
                component: () =>
                    import ("@/Views/system-user/sysuser.vue"),
            },
            //乘客
            {
                path: "/passenger",
                name: "passenger",
                component: () =>
                    import ("@/Views/passenger/passenger.vue"),
            },
            // 司机管理
            {
                path: "/driver_management",
                name: "driver_management",
                component: () =>
                    import ("@/Views/driver-management/driver-management.vue"),
            },
            // 司机账户详情
            {
                path: "/driver_account_details",
                name: "driver_account_details",
                component: () =>
                    import ("@/Views/driver-management/driver-management.vue"),
            },
            // 司机账户详情
            {
                path: "/driverra_account_detail",
                name: "driverra_account_detail",
                component: () =>
                    import ("@/Views/driver-management/driver-management.vue"),
            },
            // 订单
            {
                path: "/order_info",
                name: "order_info",
                component: () =>
                    import ("@/Views/order/order.vue"),
            },
            // 訂單詳情
            {
                path: "/orderbill",
                name: "orderbill",
                component: () =>
                    import ("@/Views/order/order.vue"),
            },
            // 訂單分賬信息
            {
                path: "/orderprofitsharing",
                name: "orderprofitsharing",
                component: () =>
                    import ("@/Views/order/order.vue"),

            },
            // 规则
            {
                path: "/cancel_rule",
                name: "cancel_rule",
                component: () =>
                    import ("@/Views/rule/rule.vue"),
            },
            {
                path: "/fee_rule",
                name: "fee_rule",
                component: () =>
                    import ("@/Views/rule/rule.vue"),
            },
            {
                path: "/profitsharingrule",
                name: "profitsharingrule",
                component: () =>
                    import ("@/Views/rule/rule.vue"),
            },
            {
                path: "/rewardrule",
                name: "rewardrule",
                component: () =>
                    import ("@/Views/rule/rule.vue"),
            },
            // 系统设置
            {
                path: "/sysdept",
                name: "sysdept",
                component: () =>
                    import ("@/Views/system-user/sysdept.vue"),
            },
            {
                path: "/sysloginlog",
                name: "sysloginlog",
                component: () =>
                    import('@/Views/system-user/sysloginlog.vue'),
            },
            {
                path: "/sysmenu",
                name: "sysmenu",
                component: () =>
                    import ("@/Views/system-user/sysmenu.vue"),
            },
            {
                path: "/sysoperlog",
                name: "sysoperlog",
                component: () =>
                    import ("@/Views/system-user/sysoperlog.vue"),
            },
            {
                path: "/syspost",
                name: "syspost",
                component: () =>
                    import ("@/Views/system-user/syspost.vue"),
            },
            {
                path: "/sysrole",
                name: "sysrole",
                component: () =>
                    import ("@/Views/system-user/sysrole.vue"),
            },
            {
                path: "/sysrolemenu",
                name: "sysrolemenu",
                component: () =>
                    import ("@/Views/system-user/sysrolemenu.vue"),
            },
            {
                path: "/sysuserrole",
                name: "sysuserrole",
                component: () =>
                    import ("@/Views/system-user/sysuserrole.vue"),
            },
            {
                path: "/sysuser",
                name: "sysuser",
                component: () =>
                    import ("@/Views/system-user/sysuser.vue"),
            },
            {
                path: "/undolog",
                name: "undolog",
                component: () =>
                    import ("@/Views/system-user/undolog.vue"),
            },
            // 优惠卷
            {
                path: "/couponinfo",
                name: "coupon",
                component: () =>
                    import ("@/Views/coupon/coupon.vue"),
            },
            // 优惠卷
            {
                path: "/customercoupon",
                name: "customercoupon",
                component: () =>
                    import ("@/Views/coupon/coupon.vue"),
            },
            // 其他设置
            {
                path: "/other_setting",
                name: "other_setting",
                component: () =>
                    import ("@/Views/other-setting/other-setting.vue"),
            },
            {
                path:'/system-setting',
                name:'system-setting',
                component:()=> import('@/Views/system-setting/system-setting.vue')
            }
        ],
    },
    {
        path: "/login",
        name: "login",
        component: Login,
    },
];

const router = new VueRouter({
    routes,
});
import  store from "@/store";
// 路由守卫:全局前置导航守卫
router.beforeEach((to, from, next) => {
    // 添加导航开始时间戳
    to.meta.navigationStart = Date.now()
    console.log(`[${new Date().toISOString()}] 开始导航到: ${to.path}`)
    // 获取token
    const token =store.getters.getToken ;

    if (!token && to.name !== "login") {
        next({ path: '/login' });
    }else if (token && to.path == "/")
    {
        next({ name: 'home' });
    }else if (token && to.name === "login") {
        next({ name: "home" });
    } else {
            try {
                next();
            } catch (error) { 
                 Message.error("出现了错误")
            }
     
    }
});

// 全局后置钩子 - 记录结束时间
router.afterEach((to, from) => {
    const endTime = Date.now()
    const duration = endTime - to.meta.navigationStart
    console.log(`[${new Date().toISOString()}] 导航完成到: ${to.path}, 耗时: ${duration}ms`)
})
  
 
export default router;